package com.sky.happygos.ui.activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.sky.happygos.R;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.Constants;
import com.sky.happygos.model.Goods;
import com.sky.happygos.utils.DensityUtil;

import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/14.
 */

public class GoodsAdapter extends RecyclerView.Adapter{
    private static final int FOOTER = 1;
    private static final int NOMAL = 0;

    public Context context;
    public LayoutInflater layoutInflater;
    public List<Goods> list;
    public MyItemClickListener myItemClickListener;

    public interface MyItemClickListener {
        public void onItemClick(View view,int postion);
    }


    public GoodsAdapter(Context context, List<Goods> list){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    public void setMyItemClickListener(MyItemClickListener myItemClickListener){
        this.myItemClickListener = myItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NOMAL) {
            return new GoodsHolder(layoutInflater.inflate(R.layout.item_goods, null),myItemClickListener);
        }
        if(viewType == FOOTER){
            return new FooterHolder(layoutInflater.inflate(R.layout.item_load_footer, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == list.size()){
            return;
        }

        if(holder instanceof GoodsHolder) {
            ((GoodsHolder) holder).bindData(list.get(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == list.size()){
            return FOOTER;
        }
        return NOMAL;
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    class GoodsHolder extends RecyclerView.ViewHolder{
        SimpleDraweeView img;
        TextView name;
        TextView info;
        TextView price;
        TextView zan;


        MyItemClickListener myItemClickListenerr;

        public GoodsHolder(View itemView, final MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListenerr = myItemClickListener;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemClickListenerr.onItemClick(v,getPosition());
                }
            });

            img = (SimpleDraweeView) itemView.findViewById(R.id.iv_goods_icon);
            RoundingParams roundingParams = RoundingParams.fromCornersRadii(DensityUtil.dip2px(itemView.getContext(),5),DensityUtil.dip2px(itemView.getContext(),5),0f,0f);
            img.getHierarchy().setRoundingParams(roundingParams);
            name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            info = (TextView) itemView.findViewById(R.id.tv_goods_info);
            price = (TextView) itemView.findViewById(R.id.tv_goods_price);
            zan = (TextView) itemView.findViewById(R.id.tv_goods_zan);
        }


        public void bindData(Goods goods){
            img.setImageURI(ImagePath.to(goods.icon));
            name.setText(goods.name);
            info.setText(goods.info);
            price.setText(String.format("%.2f", goods.price));
            zan.setText(String.format("%d", goods.likeCount));
        }
    }
    class FooterHolder extends RecyclerView.ViewHolder{

        public FooterHolder(View itemView) {
            super(itemView);
        }

        public void bindData(){

        }
    }
}
